/**
 * @file   1039.cpp
 * @author Shuang Hu <hsmath@ubuntu>
 * @date   Wed Jul 14 05:10:17 2021
 * 
 * @brief  PAT advanced 1039
 * 
 * 
 */
#include<iostream>
#include<map>
#include<string>
#include<set>
using namespace std;
int main(){
    int stunum,coursenum;
    cin>>stunum>>coursenum;
    map<string,set<int>> choose;
    for(int i=0;i<coursenum;i++){
	int courseid;
	int stus;
	cin>>courseid>>stus;
	for(int j=0;j<stus;j++){
	    string s;
	    cin>>s;
	    choose[s].insert(courseid);
	}
    }
    for(int i=0;i<stunum;i++){
	string name;
	cin>>name;
	cout<<name;
	int num=choose[name].size();
	cout<<" "<<num;
	for(auto it=choose[name].begin();it!=choose[name].end();it++){
	    cout<<" "<<*it;
	}
	cout<<endl;
    }
}
